/*
 * Main.java
 * Written by: Clinton Nolan
 * Created on July 25, 2008
 *
 * The main program, creates the environment and passes control over to PlayArea
 */

import LuaSystem.LuaEngine;
import javax.swing.*;
import java.awt.event.*;
import LuaSystem.LuaEngine;
import ScreenSystem.ScreenStack;
import ImageSystem.ImageRegistry;
import java.util.*;
import java.io.*;
import ScreenSystem.Screen;

public class Main {
    
    public static void main(String[] args) {
        //Initialize Services
        LuaEngine.initializeLua();
        ScreenStack.initializeStack();
        ImageRegistry.initializeRegistry();
        
        //read main configuration file
        boolean retval = readMainConfig();
        if(retval == false)return;
        
        //register data files
        readDataRegistry();
        readScreenRegistry();
        
        //run initial lua setup file
        if(Options.luastart != null)LuaEngine.runLuaFile(Options.getLuaPath()+Options.luastart);
        
        //push initial screen onto screen stack
        Screen s = ScreenStack.getScreen(Options.menustart);
        if(s == null){
            System.err.println("Failed to open screen "+Options.menustart+"!");
            return;
        }
        ScreenStack.push(s);
        
        //sets up game window
        JFrame frame = new JFrame(Options.gamename);
        frame.setSize(Options.xres,Options.yres);
        //starts play area creation
        PlayArea pa = new PlayArea(frame);
        frame.add(pa);
        //set other things and display window
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.validate();
        frame.setVisible(true);
        frame.pack();
    }
    
    //read game main configuration file
    public static boolean readMainConfig(){
        File mconf = new File("main.conf");
        Scanner sc = null;
        try{
            sc = new Scanner(mconf);
        }catch(FileNotFoundException e){
            System.err.println("Could not open main.conf!");
            return false;
        }
        sc.useDelimiter("\n");
        System.out.println("Main Configuration");
        while(sc.hasNext()){
            String s = sc.next();
            if(s.startsWith("#"))continue;
            if(s.length() == 1 || s.length() == 0)continue;
            String c = s.substring(0,s.indexOf(' ')).toLowerCase();
            String val = null;
            if(sc.hasNext()){
                val = s.substring(s.indexOf(' ')+1,s.length()-1);
            }else{
                val = s.substring(s.indexOf(' ')+1);
            }
            
            System.out.println(c+"="+val);
            
            if(c.equals("name")){
                Options.gamename = val;
            }else if(c.equals("version")){
                Options.version = val;
            }else if(c.equals("creator")){
                Options.creator = val;
            }else if(c.equals("resolution")){
                Options.xres = Integer.parseInt(val.substring(0,val.indexOf('x')));
                Options.yres = Integer.parseInt(val.substring(val.indexOf('x')+1,val.length()));
            }else if(c.equals("gamefolder")){
                Options.gamepath = val;
            }else if(c.equals("screenfolder")){
                Options.screenpath = val;
            }else if(c.equals("luafolder")){
                Options.luapath = val;
            }else if(c.equals("datafolder")){
                Options.datapath = val;
            }else if(c.equals("luastart")){
                Options.luastart = val;
            }else if(c.equals("menustart")){
                Options.menustart = val;
            }
        }
        sc.close();
        
        return true;
    }
    
    //read the data registry file
    public static boolean readDataRegistry(){
        File dconf = new File(Options.getDataPath()+"registry.dat");
        Scanner sc = null;
        try{
            sc = new Scanner(dconf);
        }catch(FileNotFoundException e){
            System.err.println("Could not open "+Options.getDataPath()+"registry.dat!");
            return false;
        }
        
        sc.useDelimiter("\n");
        
        System.out.println("Data Registry");
        while(sc.hasNext()){
            String s = sc.next();
            String k = s.substring(0,s.indexOf(' ')).toLowerCase();
            String val = null;
            if(sc.hasNext()){
                val = s.substring(s.indexOf(' ')+1,s.length()-1);
            }else{
                val = s.substring(s.indexOf(' ')+1);
            }
            
            ImageRegistry.registerImage(k,Options.getDataPath()+val);
            
            System.out.println(k+"="+Options.getDataPath()+val);
        }
        
        return true;
    }
    
    //read the screen registry file
    public static boolean readScreenRegistry(){
        File sconf = new File(Options.getScreenPath()+"registry.dat");
        Scanner sc = null;
        try{
            sc = new Scanner(sconf);
        }catch(FileNotFoundException e){
            System.err.println("Could not open "+Options.getScreenPath()+"registry.dat!");
            return false;
        }
        
        sc.useDelimiter("\n");
        
        System.out.println("Screen Registry");
        while(sc.hasNext()){
            String s = sc.next();
            String k = s.substring(0,s.indexOf(' ')).toLowerCase();
            String val = null;
            if(sc.hasNext()){
                val = s.substring(s.indexOf(' ')+1,s.length()-1);
            }else{
                val = s.substring(s.indexOf(' ')+1);
            }
            
            ScreenStack.registerScreen(k,Options.getScreenPath()+val);
            
            System.out.println(k+"="+Options.getScreenPath()+val);
        }
        
        return true;
    }
}